import { tips } from "@/api/account-manage";
import user from "./user";
const state = {
  accountStatus: false, // 用户开户状态，1为开户绑卡成功，其他状态都需要弹窗
  openAccpuntUser: "", // 开户人
  accountType: "", // 个人 、企业
  bindPhone: "", //开户手机号
};

const mutations = {
  SET_STATUS: (state, status) => {
    state.accountStatus = status;
  },
  SET_TYPE: (state, accountType) => {
    state.accountType = accountType;
  },
  SET_ACCOUNTUSER: (state, openAccpuntUser) => {
    state.openAccpuntUser = openAccpuntUser;
  },
  SET_BINDPHONE: (state, bindPhone) => {
    //开户手机号
    state.bindPhone = bindPhone;
  },
};

const actions = {
  SET_STATUS({ commit }) {
    return new Promise((resolve, reject) => {
      tips
        .getUserAcctInfo({
          userId: user.state.info.adminId,
        })
        .then((res) => {
          if (res.data.accountInfo) {
            console.log('a')
            let { accountInfo } = res.data;
            commit(
              "SET_STATUS",
              accountInfo.status == "1" ||
                (accountInfo.type == "2" && accountInfo.status == "6")
            );
            commit(
              "SET_ACCOUNTUSER",
              accountInfo.legalName || accountInfo.name
            ); // 没有法人就取个人姓名
            commit("SET_TYPE", accountInfo.type);  //  2是个人  1是企业
            commit("SET_BINDPHONE", accountInfo.bindPhone); //开户手机号
          } else {
            console.log('b')
            commit("SET_STATUS", "-1"); // -1代表没有提交过信息
          }
          console.log('0000')
          console.log(state)
          resolve(res);
        })
        .catch((err) => reject(err));
    });
  },
  SET_ACCOUNTSTATUS({ commit }, status) {
    // 签约成功绑定手机号时，手动设置状态值
    return new Promise((resolve) => {
      commit("SET_STATUS", status);
      resolve();
    });
  },
  // 设置开户人
  SET_ACCOUNTUSER({ commit }, openAccpuntUser) {
    commit("SET_ACCOUNTUSER", openAccpuntUser);
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
